package com.jeecg.util;

import java.io.File;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

public class ClassUtil {
	/**
	 * 根据包名获取包下类名
	 * @param packageName 包名
	 * @param isRecursion 是否递归
	 * @return
	 */
	public static Set<String> getClassName(String packageName,boolean isRecursion){
		Set<String> classNames=new HashSet<String>();
		if(packageName.endsWith(".*")){
			packageName=packageName.substring(0, packageName.indexOf(".*"));
		}
		ClassLoader loader=Thread.currentThread().getContextClassLoader();
		String packagePath=packageName.replace(".", "/");
		URL url=loader.getResource(packagePath);
		classNames=getClassNameFromDir(url.getPath(),packageName,true);
		return classNames;
	}
		
	
	/**
	 * 从项目目录下获取class名称
	 * path 文件路径
	 * @param packageName //包名
	 * @param isRecursion //是否递归
	 * @return
	 */
	public static Set<String> getClassNameFromDir(String path,String packageName,boolean isRecursion){
		Set<String> classNames=new HashSet<String>(); 
		File file=new File(path);
		File files[]=file.listFiles();
		for(File f:files){
			if(f.isDirectory()){
				if(isRecursion){
					classNames.addAll(getClassNameFromDir(f.getPath(),packageName+"."+f.getName(),true));
				
				}
			}else if(f.isFile()){
				String filename=f.getName();
				System.out.println(filename);
				if(filename.endsWith(".class"));
				{
				classNames.add(packageName+"."+filename.replace(".class",""));
				}
			}
		}
		return classNames;
	}
	
}
